package algotithm.acwing.unionfind.test837;

/**
 * @author zhouyanxiang
 * @Date 2021-01-2021/1/15-15:11
 */

import java.io.*;

public class Main {

    static class UnionFind{
        int[] p;
        int[] size;
        public UnionFind(int n) {
            p = new int[n+1];
            size = new int[n+1];
            for (int i = 1; i <= n;i++) {
                p[i] = i;
                size[i] = 1;
            }
        }

        public int find(int x) {
            if (p[x] == x) {
                return x;
            } else {
                return p[x] = find(p[x]);
            }
        }

        public void union(int x,int y) {
//            if (find(x) == find(y)) {
//                return;
//            } else {
//                p[find(x)] = find(y);
//                size[find(y)] += size[find(x)];
//            }
            int f1 = find(x);
            int f2 = find(y);
            if (f1 == f2) {
                return;
            } else {
                p[f1] = f2;
                size[f2] += size[f1];
            }

        }

    }


    public static void main(String[] args)  throws IOException{
        BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));
        String[] num = reader.readLine().split(" ");
        int n = Integer.parseInt(num[0]);
        int m = Integer.parseInt(num[1]);

        UnionFind u = new UnionFind(n);

        while (m-- > 0) {
            num = reader.readLine().split(" ");
            if (num[0].equals("C")) {
                int x = Integer.parseInt(num[1]);
                int y = Integer.parseInt(num[2]);
                u.union(x,y);
            } else if (num[0].equals("Q1")) {
                int x = Integer.parseInt(num[1]);
                int y = Integer.parseInt(num[2]);
                if (u.find(x) == u.find(y)) {
                    System.out.println("Yes");
                } else {
                    System.out.println("No");
                }
            } else {
                int f = u.find(Integer.parseInt(num[1]));
                System.out.println(u.size[f]);
            }

        }
        reader.close();
    }

}
